set(SOF_TOPOLOGY_BINARY_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")

# This use of VERBOSE relies on original CMake behavior.
# From the add_custom_command() manual:
#
#    Use of VERBATIM is recommended as it enables correct behavior.
#    When VERBATIM is not given the behavior is platform specific because
#    there is no protection of tool-specific special characters.
#
# This is fine because:
# - We don't expect alsatplg to work on Windows any time soon.
# - CMake is too afraid to remove the original, no-VERBATIM behavior:
#      https://gitlab.kitware.com/cmake/cmake/issues/18849
#
# Also note that in alsa-utils commit v1.2.2~15-gcbabe7a3f0cc, alsatplg
# (accidentally?) started ignoring the verbosity level, now it's just
# verbose or not.
macro(add_alsatplg_command)
	add_custom_command(
		MAIN_DEPENDENCY ${ARGV0}
		OUTPUT ${ARGV1}
		# Warning: before alsa-utils fix 8e71fba810b87c,
		# permissions are hardcoded and only the user can read
		# the -o(utput) file.
		# See bug https://github.com/alsa-project/alsa-utils/issues/126
		COMMAND alsatplg \$\${VERBOSE:+-v 1} -c ${ARGV0} -o ${ARGV1}
		USES_TERMINAL
	)
endmacro()

macro(add_alsatplg2_command conf_header conf_target input_name output_name include_path)
	# command line definitions are optional
	# Set defines if there is an optional argument
	if (${ARGC} GREATER 5)
		set (defines ${ARGV5})
	else()
		set (defines "")
	endif()

	add_custom_command(
		MAIN_DEPENDENCY ${input_name}.conf
		# The conf_header file is the real dependency that
		# triggers a rebuild. conf_target is the target that
		# avoids a race to rebuild conf_header. See the CMake FAQ
		# or (better) Sam Thursfield's blog about custom
		# targets.
		DEPENDS ${conf_header} ${conf_target}
		OUTPUT ${output_name}.tplg

		COMMAND cat ${conf_header} ${input_name}.conf > ${output_name}.conf

		# -p to pre-process Topology2.0 conf file
		COMMAND ALSA_CONFIG_DIR=${CMAKE_SOURCE_DIR}/topology/topology2 alsatplg \$\${VERBOSE:+-v 1}
		        -I ${include_path} -D "'${defines}'" -p -c ${output_name}.conf -o ${output_name}.tplg
		USES_TERMINAL
		)
endmacro()


add_custom_target(topologies ALL)
add_dependencies(topologies topologies1)

add_subdirectory(topology1)
add_subdirectory(topology2)
